package com.candy.test;

import com.candy.dao.AccountDao;
import com.candy.dao.impl.AccountDaoImpl;
import com.candy.model.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

/**
 *
 @ContextConfiguration 注解：
 locations 属性：用于指定配置文件的位置。如果是类路径下，需要用 classpath:表明
 classes  属性：用于指定注解的类。当不使用 xml 配置时，需要用此属性指定注解类的位置。
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class Client {
    @Autowired
    AccountService accountService;
    @Autowired
    JdbcTemplate jdbcTemplate;


    @Test
    public void test06(){
       accountService.save("tom","lily",100);

    }


    @Test
    public void test05(){
        String sql="select * from userinfo";
        List ls=jdbcTemplate.queryForList(sql);
        for (Object ob:ls
             ) {
            System.out.println(ob);
        }


    }





    @Test
    public void test03(){
//        accountService.save();


    }
    public static void test04() {
//
//        AccountDao accountDao= ac.getBean("accountDao", AccountDaoImpl.class);
//        AccountDao accountDao2= ac.getBean("accountDao", AccountDaoImpl.class);
//        System.out.println(accountDao==accountDao2);
////        关闭容器，单例模式下对象销毁
//        ac.close();

    }
    public static void test02() {
        //1.使用 ApplicationContext 接口，就是在获取 spring 容器
//        ApplicationContext ac=  new FileSystemXmlApplicationContext("C:\\Users\\Administrator\\Desktop\\beans.xml");
        ApplicationContext ac= new  ClassPathXmlApplicationContext("spring-beans.xml");
//        ClassPathXmlApplicationContext ac= new  ClassPathXmlApplicationContext("beans.xml");

        //2.根据 bean 的 id 获取对象
        AccountService accountService=(AccountService)ac.getBean("accountService");
        System.out.println(accountService);
//        accountService.save();



//        关闭容器，单例模式下对象销毁
//        ac.close();
    }
    public static void test() {
        //1.使用 ApplicationContext 接口，就是在获取 spring 容器
//        ApplicationContext ac=  new FileSystemXmlApplicationContext("C:\\Users\\Administrator\\Desktop\\beans.xml");
        ApplicationContext ac= new  ClassPathXmlApplicationContext("beans.xml");
//        ClassPathXmlApplicationContext ac= new  ClassPathXmlApplicationContext("beans.xml");

        //2.根据 bean 的 id 获取对象
        AccountService accountService=(AccountService)ac.getBean("accountService");
        System.out.println(accountService);

       AccountDao accountDao= ac.getBean("accountDao", AccountDaoImpl.class);
        System.out.println(accountDao);
//        关闭容器，单例模式下对象销毁
//        ac.close();

    }
}
